﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class EquipItem : BagItem, IBeginDragHandler,IDragHandler,IEndDragHandler
{
    private Image img;
    //标记当前父物体
    public Transform parent;

    private GameObject zhuangbei;
    private void Awake()
    {
        img = GetComponent<Image>();
        zhuangbei = GameObject.Find("ZhuangBei");
    }

    // Start is called before the first frame update
    void Start()
    {
        parent = transform.parent;
    }

    // Update is called once per frame
    void Update()
    {

    }

    //开始拖拽
    public void OnBeginDrag(PointerEventData eventData)
    {
        //记录当前父对象
        parent = transform.parent;
        //设置新的父对象，拖到根目录下。 防止因为层级关系隐藏当前对象
        transform.SetParent(transform.root);

        //将当前图片的射线检测暂时关掉
        img.raycastTarget = false;
    }

    //拖拽中
    public void OnDrag(PointerEventData eventData)
    {
        Vector2 translatePoint2 = Vector2.zero;

        //将屏幕坐标转换为UI中的本地坐标
        RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent as RectTransform, //父对象
                                                    Input.mousePosition, //屏幕坐标
                                                    Camera.main,         //当前画布模式中选择的渲染摄像机
                                                    out translatePoint2); //输入结果

        //设置本地坐标
        transform.localPosition = translatePoint2;

        Debug.Log(transform.position);
    }

    //结束拖拽
    public void OnEndDrag(PointerEventData eventData)
    {
        //将当前图片的射线检测开启
        img.raycastTarget = true;

        //获取Item脚本
        BagItem bagItem = eventData.pointerEnter.GetComponent<BagItem>();

        //下面的对象是其他对象
        if (bagItem == null)
        {
            //回到原始位置
            BagSingleTon.instance.SetEquipToBox(transform,transform.parent);
        }
       
        else 
        {
            //执行接收方法
            bagItem.Receive(this);
        }
    }

    //当拖拽装备时，移动到了其他装备上
    //装备栏
    public override void Receive(EquipItem equip)
    {
        //获取父对象
        BoxItem myBox = parent.GetComponent<BoxItem>();
        BoxItem otherBox = equip.parent.GetComponent<BoxItem>();

        //如果其中一个装备在装备栏
        if (myBox.equipType != EquipType.None || otherBox.equipType != EquipType.None)
        {
            if (equipType != equip.equipType)
            {
                //各自接收格子的装备
                myBox.Receive(this);
                otherBox.Receive(equip);
                return;
            }
        }

        //先修改标志位
        myBox.isExchange = true;
        otherBox.isExchange = true;

        //再去进行互相交换
        myBox.Receive(equip);
        otherBox.Receive(this);
    }


    public void ZhuanBeiButton()
    {
        //Debug.Log("装备栏");
        //zhuangbei.active = true;
        //zhuangbei.transform.position = new Vector2(transform.position.x, transform.position.y);
    }

}
